/******************************************************************************
@file:  LoginHandler.java
@author Ramesh Anandhan
DESCRIPTION
    this class parses the login xml response.
-----------------------------------------------------------------------------
Copyright (c) 2011, GEEKWAVE SOLUTIONS INC.

All rights reserved.
 ******************************************************************************/
package com.smartc.connection;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

/*
 * parses the login http connection response entity
 */
public class LoginHandler extends DefaultHandler {

	//private boolean mLogin = false;
	private boolean mIdUser = false;
	private boolean mStatus = false;
	private boolean mMessage = false;
	private ParsedLoginData myParsedLoginDataSet = null;

	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {
		if (this.mStatus) {
			myParsedLoginDataSet.setStatus(new String(ch, start, length));
		} else if (this.mMessage) {
			myParsedLoginDataSet.setMessage(new String(ch, start, length));
		} else if (this.mIdUser) {
			myParsedLoginDataSet.setUserId(new String(ch, start, length));
		}
	}

	@Override
	public void endDocument() throws SAXException {
	}

	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {
		if (localName.equals("login")) {
			//this.mLogin = false;
		} else if (localName.equals("status")) {
			this.mStatus = false;
		} else if (localName.equals("message")) {
			this.mMessage = false;
		} else if (localName.equals("idUser")) {
			this.mIdUser = false;
		}
	}

	@Override
	public void startDocument() throws SAXException {
		myParsedLoginDataSet = new ParsedLoginData();
	}

	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {
		if (localName.equals("login")) {
			//this.mLogin = true;
		} else if (localName.equals("status")) {
			this.mStatus = true;
		} else if (localName.equals("message")) {
			this.mMessage = true;
		} else if (localName.equals("idUser")) {
			this.mIdUser = true;
		}
	}

	public ParsedLoginData getParsedLoginData() {
		return new ParsedLoginData(myParsedLoginDataSet);
	}
}
